﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Net;
using System.Net.Mail;

namespace SampleFacebookApp.Helpers
{
    public class DbHelper
    {
        public static string SavePath = HttpContext.Current.Server.MapPath("~/App_Data/Data.xml");

        public DbHelper()
        {
        }

        public void AddRecord(IDictionary<string, string> data)
        {
            DataTable dtData = new DataTable();
            dtData.Columns.AddRange(new DataColumn[] 
            { 
                new DataColumn("id"),
                new DataColumn("Name"),
                new DataColumn("Age"),
                new DataColumn("Dob"),
                new DataColumn("Email"),
                new DataColumn("Address"),
                new DataColumn("District"),
                new DataColumn("Mobile"),
                new DataColumn("AceessToken"),
                new DataColumn("DateTime")
            });
            dtData.TableName = "DtNew";
            dtData.AcceptChanges();
            if (!System.IO.File.Exists(SavePath))
            {
                dtData.WriteXml(SavePath);
            }
            dtData.ReadXml(SavePath);

            System.Data.DataRow drNew = dtData.NewRow();

            emailnotify objemailnotify = new emailnotify(sendnotification);
            var result = objemailnotify.BeginInvoke(data["Email"].ToString(), data["Name"].ToString(), new AsyncCallback(CallbackMethod), null);

            drNew["id"] = (data["id"] != null) ? data["id"].ToString() : "";
            drNew["Name"] = (data["Name"] != null) ? data["Name"].ToString() : "";
            drNew["Age"] = (data["Age"] != null) ? data["Age"].ToString() : "";
            drNew["Dob"] = (data["Dob"] != null) ? data["Dob"].ToString() : "";
            drNew["Email"] = (data["Email"] != null) ? data["Email"].ToString() : "";
            drNew["Address"] = (data["Address"] != null) ? data["Address"].ToString() : "";
            drNew["District"] = (data["District"] != null) ? data["District"].ToString() : "";
            drNew["Mobile"] = (data["Mobile"] != null) ? data["Mobile"].ToString() : "";
            drNew["AceessToken"] = (data["AceessToken"] != null) ? data["AceessToken"].ToString() : "";
            drNew["DateTime"] = DateTime.Now;

            dtData.Rows.Add(drNew);
            dtData.AcceptChanges();
            dtData.TableName = "DtNew";
            dtData.WriteXml(SavePath);
            dtData.Dispose();
        }

        public double distinctcount()
        {
            DataTable dtData = new DataTable();
            dtData.Columns.AddRange(new DataColumn[] 
            { 
                new DataColumn("id"),
                new DataColumn("Name"),
                new DataColumn("Age"),
                new DataColumn("Dob"),
                new DataColumn("Email"),
                new DataColumn("Address"),
                new DataColumn("District"),
                new DataColumn("Mobile"),
                new DataColumn("AceessToken"),
                new DataColumn("DateTime")
            });
            dtData.TableName = "DtNew";
            dtData.AcceptChanges();
            if (!System.IO.File.Exists(SavePath))
            {
                dtData.WriteXml(SavePath);
            }
            dtData.ReadXml(SavePath);
            var distinctValues = dtData.AsEnumerable()
                        .Select(row => new
                        {
                            id = row.Field<string>("id"),
                        })
                        .Distinct();
            dtData.Dispose();
            return distinctValues.Count();
        }

        #region "Subscriptions email"
        public delegate void emailnotify(string email, string username);

        static void CallbackMethod(IAsyncResult ar)
        {
            AsyncResult result = (AsyncResult)ar;
            emailnotify caller = (emailnotify)result.AsyncDelegate;
            caller.EndInvoke(ar);
        }
        void sendnotification(string email, string username)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<table border=\"1\"  style=\"border:2px solid black;\"><tr><td>");
            sb.Append("<br/><br/>");
            sb.AppendFormat("Dear {0},", username);
            sb.Append("<br/><br/>");
            sb.Append("Thank you for Registring in AAP-WB Facebook App <br/>");
            sb.Append("<br/><br/>");
            sb.AppendFormat("Our co-orinator will contact you for formal membership");
            sb.Append("<br/><br/>");
            sb.Append("Thank you! Jai Hind<br/>");
            sb.Append("Team AAP-WB");
            sb.Append("www.aamaadmipartywestbengal.org");
            sb.Append("<br/><br/>");
            sb.Append("</td></tr></table>");

            var fromAddress = new MailAddress("aapwestbengal@gmail.com", "Aam Aadmi Party - West Bengal");
            var toAddress = new MailAddress(email, username);
            const string fromPassword = "a.arvind";
            const string subject = "Registration confirmed";

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };

            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = sb.ToString()
            })
            {
              //  smtp.Send(message);
            }
        }
        #endregion
    }

}